package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.common.utils.transfer.GetTime;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.common.utils.transfer.RandomUtils;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.handler.InterfaceHandler;

import java.util.Map;

@SuppressWarnings("all")
@MarkAsApi
public class BabyRecord extends InterfaceHandler {
    final String insertSql = "insert into t_baby_record(sequence,start_time,end_time,content,create_time,update_time) values(?,?,?,?,?,?)";
    final String deleteSql = "delete from t_baby_record where sequence=?";
    final String updateSql = "update t_baby_record set start_time=?,end_time=?,content=?,update_time=? where sequence=?";
    final String selectSql = "select * from t_baby_record where create_time BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW() order by create_time";

    @Override
    public Object execute(Map<String, Object> paramsMap) {
        boolean insert = Verification.checkNull(paramsMap.get("sequence"));
        if (insert) paramsMap.put("sequence", RandomUtils.sequence());

        final String type = Transformation.toString(paramsMap.remove("type"));
        final String sequence = paramsMap.get("sequence").toString();

        if ("update".equals(type)) {
            final String startTime = paramsMap.get("startTime").toString().replace("：", ":").replaceAll(":{2,}", ":").replaceAll("\\s+", " ");
            final String endTime = paramsMap.get("endTime").toString().replace("：", ":").replaceAll(":{2,}", ":").replaceAll("\\s+", " ");
            final String content = paramsMap.get("content").toString();
            if (insert) {
                jdbcTemplate.update(insertSql, sequence, startTime, endTime, content, GetTime.stringTime(), GetTime.stringTime());
            } else {
                jdbcTemplate.update(updateSql, startTime, endTime, content, GetTime.stringTime(), sequence);
            }
            return true;
        } else if ("delete".equals(type)) {
            jdbcTemplate.update(deleteSql, sequence);
            return true;
        } else {
            return jdbcTemplate.queryForList(selectSql);
        }
    }
}
